package com.sgb.kjwl.view.widget.lease;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.lease.DictEntity4;
import com.sgb.kjwl.model.entity.request.MachineFilterJson3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class DeviceFliterPopWindow3 extends BasePopWindow {
    private LinearLayout rootLayout;
    private TextView typeView;
    private TextView modelView;
    private TextView timeView;
    private TextView sortView;
    private TextView okView;
    private TextView resetView;
    private MaxRecyclerView typeRecyclerView;
    private MaxRecyclerView modelRecyclerView;
    private MaxRecyclerView timeRecyclerView;
    private MaxRecyclerView sortRecyclerView;
    private MaterialFilterAdapter3 typeAdapter;
    private MaterialFilterAdapter3 modelAdapter;
    private MaterialFilterAdapter3 timeAdapter;
    private MaterialFilterAdapter3 sortAdapter;
    private FilterResultClick filterResultClick;
    private MachineFilterJson3 machineFilterJson3;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;
    private List<DictEntity4> tempDictList;
    private List<MaterialFilterAdapter3> adapterList;
    private boolean isFir = true;

    public interface FilterResultClick {
        void result(MachineFilterJson3 machineFilterJson3, List<DictEntity3> data, SparseBooleanArray checkBooleanArray);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public DeviceFliterPopWindow3(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_device_fliter3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);

        typeView = holder.getPop().findViewById(R.id.type_view);
        modelView = holder.getPop().findViewById(R.id.model_view);
        timeView = holder.getPop().findViewById(R.id.time_view);
        sortView = holder.getPop().findViewById(R.id.sort_view);

        typeRecyclerView = holder.getPop().findViewById(R.id.type_list);
        modelRecyclerView = holder.getPop().findViewById(R.id.model_list);
        timeRecyclerView = holder.getPop().findViewById(R.id.time_list);
        sortRecyclerView = holder.getPop().findViewById(R.id.sort_list);

        typeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        modelRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        timeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        sortRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));

        typeAdapter = new MaterialFilterAdapter3(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    filterResultClick.queryTypeChild(materialFilterEntity.getId());
                }
                machineFilterJson3.setTypeId(materialFilterEntity.getId());
            }
        });
        typeRecyclerView.setAdapter(typeAdapter);

        modelAdapter = new MaterialFilterAdapter3(mContext, null);
        modelAdapter.setItemSelect(true);
        modelAdapter.setDefItem(-1);
        modelAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    filterResultClick.queryNameChild(materialFilterEntity.getId());
                }
                machineFilterJson3.setModelId(materialFilterEntity.getId());
                machineFilterJson3.setSecondCategoryName(materialFilterEntity.getName());
            }
        });
        modelRecyclerView.setAdapter(modelAdapter);

        timeAdapter = new MaterialFilterAdapter3(mContext, null);
        timeAdapter.setItemSelect(true);
        timeAdapter.setDefItem(-1);
        timeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                machineFilterJson3.setMachineAgeId(materialFilterEntity.getId());
            }
        });
        timeRecyclerView.setAdapter(timeAdapter);

        sortAdapter = new MaterialFilterAdapter3(mContext, null);
        sortAdapter.setItemSelect(true);
        sortAdapter.setDefItem(-1);
        sortAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                machineFilterJson3.setSortId(materialFilterEntity.getId());
                machineFilterJson3.setSortName(materialFilterEntity.getName());
            }
        });
        sortRecyclerView.setAdapter(sortAdapter);

        machineFilterJson3 = new MachineFilterJson3();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();
        tempDictList = new ArrayList<>();
        adapterList = new ArrayList<>();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                tempMaterKeyList.clear();
                tempMaterValueList.clear();
                tempDictList.clear();
                for (int i = 0; i < materValueList.size(); i++) {
                    if (!materValueList.get(i).equals("")) {
                        tempMaterValueList.add(materValueList.get(i));
                        tempMaterKeyList.add(materKeyList.get(i));
                        DictEntity4 dictEntity4 = new DictEntity4();
                        dictEntity4.setKey(materKeyList.get(i));
                        dictEntity4.setValue(materValueList.get(i));
                        tempDictList.add(dictEntity4);
                    }
                }
                machineFilterJson3.setAttrList(tempDictList);
                if (filterResultClick != null) {
                    filterResultClick.result(machineFilterJson3, null, null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {
        for (MaterialFilterAdapter3 adapter : adapterList) {
            adapter.setDefItem(-1);
        }
        for (int i = 0; i < materValueList.size(); i++) {
            materValueList.set(i, "");
        }
        machineFilterJson3.setAttrList(null);
        areaList.clear();

        typeAdapter.setDefItem(0);
        machineFilterJson3.setTypeId(typeAdapter.getDatas().get(0).getId());

        modelAdapter.setDefItem(0);
        machineFilterJson3.setModelId(modelAdapter.getDatas().get(0).getId());
        machineFilterJson3.setSecondCategoryName(modelAdapter.getDatas().get(0).getName());

        sortAdapter.setDefItem(0);
        machineFilterJson3.setSortId(sortAdapter.getDatas().get(0).getId());
        machineFilterJson3.setSortName(sortAdapter.getDatas().get(0).getName());
    }

    public void setTypeData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            typeView.setVisibility(View.GONE);
        } else {
            typeView.setVisibility(View.VISIBLE);
        }
        if (isFir) {
            typeAdapter.setDefItem(0);
            machineFilterJson3.setTypeId(data.get(0).getId());
            isFir = false;
        }
        typeAdapter.setDatas(data);
    }

    public void setTypeDefault(int position, String id) {
        typeAdapter.setDefItem(position);
        machineFilterJson3.setTypeId(id);
    }

    public void setModelData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            modelView.setVisibility(View.GONE);
            return;
        } else {
            modelView.setVisibility(View.VISIBLE);
        }
        modelAdapter.setDefItem(0);
        machineFilterJson3.setModelId(data.get(0).getId());
        modelAdapter.setDatas(data);
    }

    public void setModelDefault(int position, String id) {
        modelAdapter.setDefItem(position);
        machineFilterJson3.setModelId(id);
    }

    public void setTimeData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            timeView.setVisibility(View.GONE);
        } else {
            timeView.setVisibility(View.VISIBLE);
        }
        timeAdapter.setDatas(data);
    }

    public void setTimeDefault(int position, String id) {
        timeAdapter.setDefItem(position);
        machineFilterJson3.setMachineAgeId(id);
    }

    public void setSortData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            sortView.setVisibility(View.GONE);
        } else {
            sortView.setVisibility(View.VISIBLE);
        }
        sortAdapter.setDatas(data);
    }

    public void setSortDefault(int position, String id) {
        sortAdapter.setDefItem(position);
        machineFilterJson3.setPriceSort(id);
    }

    public void setSpecData(List<DictEntity3> data) {
        adapterList.clear();
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getId());
            materValueList.add("");
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
            addLineView();
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(12);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_999999));
        textView.setPadding(0,mContext.getResources().getDimensionPixelOffset(R.dimen.d10),0,mContext.getResources().getDimensionPixelOffset(R.dimen.d10));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter3 childAdapter = new MaterialFilterAdapter3(mContext, null);
        childAdapter.setItemSelect(true);
        childAdapter.setDefItem(-1);
        childAdapter.setIndex(index);
        adapterList.add(childAdapter);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        childAdapter.setDatas(data);
        childAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getId());
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    private void addLineView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mContext.getResources().getDimensionPixelOffset(R.dimen.d1));
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setBackgroundColor(mContext.getResources().getColor(R.color.color_F5F5F5));
        layoutParams.topMargin=mContext.getResources().getDimensionPixelOffset(R.dimen.d10);
        rootLayout.addView(textView, layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
